﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class BAKAB45AV95Report : BaseReport
    {
        #region Variable
        byte[] b;
        string strCompany = "", strExcelFile = "";
        #endregion
        public BAKAB45AV95Report()
            : base()
        {
        }
        public BAKAB45AV95Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            columns.Add("VKPWUTERM");
            columns.Add("VKPNEUDAT");
            columns.Add("VKPAUFNUM");
            columns.Add("KDKUNDNA");
            columns.Add("ARTICLE");
            columns.Add("STDBATCHNO");
            columns.Add("BSFBEZ");
            columns.Add("VWORDERNUM");
            columns.Add("ABPSTARTDAT");
            columns.Add("JSACTDATE");
            columns.Add("GPRODUCER");
            columns.Add("SPRODUCER");
            columns.Add("ABPSOLLMENGE1");
            columns.Add("ABPSOLLMENGE8");
            columns.Add("ABPLIEFDAT");
            columns.Add("ABPERLDAT");
            columns.Add("GMETER");
            columns.Add("GWARPYARNINFO");
            columns.Add("REMARK");
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                string VWORDERNUM = "",GAPOS="";
                int maxrowid = 0;
                string column;
                string rowid;
                DataTable tbABPOS = setSoruce.Tables["ABPOS"];
                DataTable tbGAAUF = setSoruce.Tables["GAAUF"];
                DataTable tbABPOSGA = tbABPOS.Clone();
                DataRow dwABPOSGA = tbABPOSGA.NewRow();
                foreach (DataRow dwABPOS in tbABPOS.Rows)
                {
                    #region ROWID
                    rowid = dwABPOS["ROWID"].ToString().Trim();
                    if (rowid == "")
                    {
                        dwABPOS["ROWID"] = (maxrowid + 1).ToString();
                        maxrowid++;
                        rowid = dwABPOS["ROWID"].ToString().Trim();
                    }
                    else
                    {
                        maxrowid = System.Convert.ToInt16(rowid);
                        rowid = maxrowid.ToString(); ;
                    }
                    #endregion
                    VWORDERNUM = dwABPOS["VWORDERNUM"].ToString().Trim();
                    GAPOS = dwABPOS["GAPOS"].ToString().Trim();
                    dwABPOSGA = tbABPOSGA.NewRow();
                    dwABPOSGA.ItemArray = dwABPOS.ItemArray;
                    #region GAAUF        
                    var query = from DataRow dwGAAUF in tbGAAUF.AsEnumerable()
                                where dwGAAUF["ZUAUFNUM"].ToString() == VWORDERNUM
                                && dwGAAUF["GAPOS"].ToString() == GAPOS
                                select dwGAAUF;
                    if (GAPOS == "")
                    {
                        query = from DataRow dwGAAUF in tbGAAUF.AsEnumerable()
                                    where dwGAAUF["ZUAUFNUM"].ToString() == VWORDERNUM 
                                    select dwGAAUF;
                    }
                 
                    if (query.Count() == 0)
                    {
                        dwABPOSGA["ROWID"] = rowid;
                        tbABPOSGA.Rows.Add(dwABPOSGA);
                        continue;
                    }
                    foreach (DataRow dwGAAUF in query)
                    {
                        dwABPOSGA["VWORDERNUM"] = VWORDERNUM;
                        dwABPOSGA["ZUAUFNUM"] = VWORDERNUM;
                        dwABPOSGA["GAPOS"] = dwGAAUF["GAPOS"];
                        foreach (DataColumn dc in tbGAAUF.Columns)
                        {
                            column = dc.ColumnName;
                            if (column == "ROWID")
                                continue;
                            if(tbABPOSGA.Columns.Contains(column))
                            {
                                dwABPOSGA[column]=dwGAAUF[column];
                            } 
                        }
                        dwABPOSGA["ROWID"] = rowid;
                        if (GAPOS == "")
                        {
                            tbABPOSGA.Rows.Add(dwABPOSGA);
                        }                        
                        dwABPOSGA = tbABPOSGA.NewRow();
                    }
                    #endregion                  
                }
                tbABPOSGA.AcceptChanges();
                exAgent.DataTableToExcel(tbABPOSGA, 3, 1);
                return;
                int x = xStart;
       
                string suppliercode;
                string ABPPOSTYP;
                int Batches; 
                string value;
                int yend = 22;
 
               
                foreach (DataRow dwTemp in tbABPOS.Rows)
                {
                    rowid = dwTemp["ROWID"].ToString();
                    if (rowid == "")
                    {
                        dwTemp["ROWID"] = (maxrowid + 1).ToString();
                        maxrowid++;
                    }
                    else
                    {
                        maxrowid = System.Convert.ToInt16(rowid);
                    }
                    yStart = 1;
                    yend = 22;
                    for (int y = yStart; y < yend; y++)
                    {
                        column = exAgent.GetCellValue(5, y).ToString();
                        if (tbABPOS.Columns.Contains(column))
                        {
                            exAgent.SetCellValue(x, y, dwTemp[column]);
                        }
                    }
                    yStart = 22;
                    yend = 35;
                    VWORDERNUM=dwTemp["VWORDERNUM"].ToString().Trim();
                    if (VWORDERNUM == "")
                    {
                        x++;
                        continue;
                    }
                    var query = from DataRow dwGAAUF in tbGAAUF.AsEnumerable()
                                where dwGAAUF["ZUAUFNUM"].ToString() == VWORDERNUM
                                select dwGAAUF;
                    if (query.Count() == 0)
                    {
                        x++;
                        continue;
                    }
                    foreach (DataRow dwGAAUF in query)
                    {
                        for (int y = yStart; y < yend; y++)
                        {
                            column = exAgent.GetCellValue(3, y).ToString();

                            if (tbGAAUF.Columns.Contains(column))
                            {
                                exAgent.SetCellValue(x, y, dwGAAUF[column]);
                            }
                        }
                        x++;
                    }
                }
                //exAgent.SetRangeValue(3, 3, 1, 50, "");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void WriteExcelReport(string fileType)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "BAKAB45AV95";
            strExcelFile = @"HLT\HLTPlanningReport.xls";
            string filePath = @"C:\intex.w\Report\" + strExcelFile;
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            //string filedate = DateTime.Now.Year.ToString();
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"Planning\浆纱\" + filedate + ".xls";
            sheetName =DateTime.Now.Hour.ToString()+"-"+DateTime.Now.Minute.ToString();
           // exAgent.DeleteWorkSheet();
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(columns, dsSource, 5,1);
            base.WriteExcelReport(fileType);
        }
    }
}
